package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;

import com.lishe.lishe.lishe.module.excitation.adapter.ViewpagerAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.DianzanImgObject;
import com.lishe.lishe.lishe.module.excitation.bean.HeadInfo;
import com.lishe.lishe.lishe.utils.BitmapUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by DELL on 2017/10/30.
 */

public class ShowFullScreenImageDialog extends Dialog {


    public ShowFullScreenImageDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements View.OnClickListener, View.OnLongClickListener {
        private Activity context;
        private ShowFullScreenImageDialog dialog;
        private List<DianzanImgObject> imgUrls;
        private List<View> views;
        private List<Bitmap> bitmaps;
        private LinkedList<View> points;
        private ViewpagerAdapter adapter;
        private int currentPosition;
        private ImageLoaderUtils utils;

        private ViewPager viewPager;
        private LinearLayout pointLayout,loading;

        public Builder(Activity context){
            this.context=context;
            utils=new ImageLoaderUtils();
            views=new ArrayList<>();
            bitmaps=new ArrayList<>();
            points=new LinkedList<>();
        }


        public Builder setBitmap(List<DianzanImgObject> imgUrls){
            this.imgUrls=imgUrls;
            createView();
            return this;
        }

        public Builder setCurrentItem(int position){
            currentPosition=position;
            return  this;
        }

        /**
         * 创建页面
         */
        private void createView() {
            views.clear();
            if(adapter==null&&viewPager==null){
                show();
            }
            new Thread(){
                @Override
                public void run() {
                    for (int i = 0; i < imgUrls.size(); i++) {
                        ImageView img = new ImageView(context);
                        Bitmap bmp=BitmapUtils.fullScreenBitmap(utils.getBitmap(imgUrls.get(i).getImgUrl().replace("-small","")),BitmapUtils.getDisplayWidth(context));
                        if(bmp!=null) {
                            bitmaps.add(bmp);
                            img.setImageBitmap(bmp);
                            img.setOnClickListener(Builder.this);
                            img.setOnLongClickListener(Builder.this);
                            views.add(img);
                        }
                    }
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(bitmaps.size()<imgUrls.size()){
                                Toast.makeText(context,"图片加载失败！",Toast.LENGTH_SHORT).show();
                            }
                            setAdapter();
                            if(loading!=null){
                                loading.setVisibility(View.GONE);
                            }
                        }
                    });
                }
            }.start();

        }



        public ShowFullScreenImageDialog create() throws NullPointerException{
            if(dialog==null) {
                dialog = new ShowFullScreenImageDialog(context, R.style.BlackDialog);
                View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_full_screen_image, null);
                viewPager = contentView.findViewById(R.id.viewPager);
                pointLayout=contentView.findViewById(R.id.point_layout);
                loading=contentView.findViewById(R.id.loading);
                pointLayout.removeAllViews();
                if(imgUrls.size()>1) {
                    for (int i = 0; i < imgUrls.size(); i++) {
                        //小圆点
                        View pointView = context.getLayoutInflater().inflate(R.layout.item_carousel_point, null);
                        View point = pointView.findViewById(R.id.point);
                        pointLayout.addView(pointView);
                        points.add(point);
                    }
                }

//                viewPager.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dissmiss();
//                    }
//                });
//
//                viewPager.setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View v) {
//                        showBottomDialog(bitmaps.get(currentPosition),url);
//                        return false;
//                    }
//                });

                dialog.setContentView(contentView);
                dialog.getWindow().setGravity(Gravity.CENTER);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(true);
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
            }
            return dialog;
        }

        private void setAdapter(){
            if(adapter==null){
                adapter=new ViewpagerAdapter(views);
            }
            viewPager.setAdapter(adapter);
            viewPager.setCurrentItem(currentPosition);
            if(points.size()==imgUrls.size())
                points.get(currentPosition).setBackgroundResource(R.drawable.shape_red50px);
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if(points.size()==imgUrls.size()) {
                        points.get(position).setBackgroundResource(R.drawable.shape_red50px);
                        points.get(currentPosition).setBackgroundResource(R.drawable.shape_coner50px);
                    }
                    currentPosition=position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }


        private void showBottomDialog(Bitmap bmp,String url) {
            ShowBottomFunctionDialog.Builder builder = new ShowBottomFunctionDialog.Builder(context);
            builder.setBitmap(bmp).setUrl(url);
            builder.show();
        }

        private void show(){
            try {
                create();
                if(dialog!=null&&!dialog.isShowing()){
                    dialog.show();
                }
            }catch (NullPointerException e){
                e.printStackTrace();
            }
        }

        private void dissmiss(){
            if(dialog!=null) {
                dialog.dismiss();
                dialog.cancel();
            }
        }

        @Override
        public void onClick(View v) {
            dissmiss();
        }

        @Override
        public boolean onLongClick(View v) {
            showBottomDialog(bitmaps.get(currentPosition),imgUrls.get(currentPosition).getImgUrl());
            return false;
        }
    }

}
